package com.dsteiner.xelavos.client;



import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.DateUtilImpl;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.PopupOverlayWidget;
import com.dsteiner.widgets.client.PrefilledPasswordTextBox;
import com.dsteiner.widgets.client.PrefilledTextBox;
import com.dsteiner.widgets.client.PrefilledTextInput;
import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.RoundBorder;
import com.dsteiner.widgets.client.RoundBorder.RoundBorderModel;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.CheckBoxExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.HorizontalPanelExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.PasswordTextBoxExt;
import com.dsteiner.widgets.client.Widgets.RadioButtonExt;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.dsteiner.widgets.client.resources.RoundBorderCssResource;
import com.dsteiner.widgets.client.resources.RoundBorderResources;
import com.dsteiner.xelavos.client.controller.AuthenticationController;
import com.dsteiner.xelavos.client.controller.CreateAccountController;
import com.dsteiner.xelavos.client.controller.LoginController;
import com.dsteiner.xelavos.client.controller.UserController;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.model.MyEntriesModel;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.dsteiner.xelavos.client.rpc.DataService;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Widget;

public class AuthenticationWidget extends LazyWidget{

	public static class CreateAccountModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
				this);

		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName,
					listener);
		}
		
		private String message;

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			String oldMessage = this.message;
			this.message = message;
			propertyChangeSupport.firePropertyChange("message", oldMessage, message);
		}
	}
	private class CreateAccounWidget extends LazyWidget{

		private CreateAccountController createAccountController = new CreateAccountController();
		private CreateAccountModel createAccountModel = new CreateAccountModel();
		private final PopupOverlayModel createAccountPopupModel;
		public CreateAccounWidget(PopupOverlayModel createAccountPopupModel) {
			this.createAccountPopupModel = createAccountPopupModel;
		}
		@Override
		public Widget createContentWidget() {
			FlowPanel mainPanel = new FlowPanel();
			FlexTable mainTable = new FlexTable();
			PrefilledTextBoxModel firstNameTextBoxModel = new PrefilledTextBoxModel();
			firstNameTextBoxModel.setDefaultText("nombre");
			PrefilledTextBoxModel lastNameTextBoxModel = new PrefilledTextBoxModel();
			lastNameTextBoxModel.setDefaultText("appellido");
			PrefilledTextBoxModel emailTextBoxModel = new PrefilledTextBoxModel();
			emailTextBoxModel.setDefaultText("email");
			PrefilledTextBoxModel nickNameTextBoxModel = new PrefilledTextBoxModel();
			nickNameTextBoxModel.setDefaultText("sobre nombre");
			PrefilledTextBoxModel passwordTextBoxModel = new PrefilledTextBoxModel();
			passwordTextBoxModel.setDefaultText("contraseña (con al menos 6 letras)");
			PrefilledTextBoxModel passwordConfirmTextBoxModel = new PrefilledTextBoxModel();
			passwordConfirmTextBoxModel.setDefaultText("repetir contraseña");
			PrefilledTextBox firstNameTextBox = new PrefilledTextBox(firstNameTextBoxModel);
			PrefilledTextBox lastNameTextBox = new PrefilledTextBox(lastNameTextBoxModel);
			PrefilledTextBox emailTextBox = new PrefilledTextBox(emailTextBoxModel);
			PrefilledTextBox nickNameTextBox = new PrefilledTextBox(nickNameTextBoxModel);
			PrefilledPasswordTextBox passwordTextBox = new PrefilledPasswordTextBox(passwordTextBoxModel);
			PrefilledPasswordTextBox passwordConfirmTextBox = new PrefilledPasswordTextBox(
					passwordConfirmTextBoxModel);
			ButtonExt createAccountButton = new ButtonExt("crear cuenta");
			InlineHTML genderHtml = new InlineHTML("sexo");
			RadioButtonExt maleRadioButton = new RadioButtonExt("gender_rbts");
			RadioButtonExt femaleRadioButton = new RadioButtonExt("gender_rbts");
			maleRadioButton.setText("hombre");
			femaleRadioButton.setText("mujer");
			
//			ListBoxWidget dayListBox = new ListBoxWidget();
//			ListBoxWidget monthListBox = new ListBoxWidget();
//			ListBoxWidget yearListBox = new ListBoxWidget();
//			for (int i = 1; i <= 31; i++) {
//				dayListBox.addItem(i+"");
//			}
//			for (int i = 1; i <= 12; i++) {
//				monthListBox.addItem(i+"");
//			}
//			for (int i = 1950; i <= 2009; i++) {
//				yearListBox.addItem(i+"");
//			}
//			HorizontalPanel birthdayPanel = new HorizontalPanel();
//			
//			birthdayPanel.add(new HTMLExt("cumplea�os"));
//			birthdayPanel.add(dayListBox);
//			birthdayPanel.add(monthListBox);
//			birthdayPanel.add(yearListBox);
			PrefilledTextBoxModel birthdayTextBoxModel = new PrefilledTextBoxModel();
			birthdayTextBoxModel.setDefaultText("cumpleaños (dd/mm/aaaa)");
			PrefilledTextBox birthdayTextBox = new PrefilledTextBox(birthdayTextBoxModel);
			CheckBoxExt termsCheckBox = new CheckBoxExt("accepto las " +
					"<a href=\"/terms.html\" target=\"_blank\">condiciones de uso</a> y " +
					"<a href=\"/privacy.html\" target=\"_blank\">politica de privacidad</a> de Xelavos");
			CheckBoxExt receiveReportCheckBox = new CheckBoxExt("quiero que Xelavos.com me mande nuevos registros " +
					"a mi correo cada semana");
			HTMLExt messageHtml = new HTMLExt();
			
			mainTable.setWidget(0, 0, firstNameTextBox);
			mainTable.setWidget(0, 1, lastNameTextBox);
//			mainPanel.setWidget(1, 0, nickNameTextBox);
			mainTable.setWidget(1, 0, emailTextBox);
			mainTable.setWidget(1, 1, birthdayTextBox);
			mainTable.setWidget(2, 0, passwordTextBox);
			mainTable.setWidget(2, 1, passwordConfirmTextBox);
			mainTable.setWidget(3, 0, termsCheckBox);
			mainTable.setWidget(4, 0, receiveReportCheckBox);
			mainTable.setWidget(5, 0, createAccountButton);
			mainTable.setWidget(6, 0, messageHtml);
			
//			mainPanel.getFlexCellFormatter().setColSpan(2, 0, 2);
			mainTable.getFlexCellFormatter().setColSpan(3, 0, 2);
			mainTable.getFlexCellFormatter().setColSpan(4, 0, 2);
			mainTable.getFlexCellFormatter().setColSpan(6, 0, 2);
			
			mainPanel.add(genderHtml);
			mainPanel.add(maleRadioButton);
			mainPanel.add(femaleRadioButton);
			mainPanel.add(mainTable);
			
			setStyleName(XelavosResources.INSTANCE.css().createAccountWidget());
			
			createAccountController.registerCreateAccountWidget(this);
			createAccountController.registerCreateAccountModel(createAccountModel);
			createAccountController.registerCreateAccountPopupModel(createAccountPopupModel);
			createAccountController.registerMessageHtml(messageHtml);
			createAccountController.registerJsUtil(JsUtil.getInstance());
			createAccountController.registerEventManager(EventManagerImpl.getInstance());
			createAccountController.registerDataService(DataService.Util.getInstance());
			createAccountController.registerDateUtil(DateUtilImpl.getInstance());
			createAccountController.registerCreateAccountButton(createAccountButton);
			createAccountController.registerMaleRadioButton(maleRadioButton);
			createAccountController.registerFemaleRadioButton(femaleRadioButton);
			createAccountController.registerFirstNameTextBox(firstNameTextBoxModel);
			createAccountController.registerLastNameTextBox(lastNameTextBoxModel);
			createAccountController.registerEmailTextBox(emailTextBoxModel);
			createAccountController.registerPasswordTextBox(passwordTextBoxModel);
			createAccountController.registerPasswordConfirmTextBox(passwordConfirmTextBoxModel);
			createAccountController.registerBirthdayTextBoxModel(birthdayTextBoxModel);
			createAccountController.registerAuthenticationModel(authenticationModel);
			createAccountController.registerTermsCheckBox(termsCheckBox);
			createAccountController.registerReceiveReportCheckBox(receiveReportCheckBox);
			return mainPanel;
		}
	}
	
	public static class UserWidget extends LazyWidget{
		UserController userController = new UserController();
		private final MyEntriesModel myEntriesModel;
		private final AuthenticationModel authenticationModel;
		
		public UserWidget(AuthenticationModel authenticationModel, MyEntriesModel myEntriesModel) {
			this.authenticationModel = authenticationModel;
			this.myEntriesModel = myEntriesModel;
		}
		
		@Override
		public Widget createContentWidget() {
//			FlowPanel mainPanel = new FlowPanel();
			HTMLExt titleHtml = new HTMLExt();
			HTMLExt myProfileLink = new HTMLExt("<a href=\"javascript:void(0);\">mi perfil</a>");
			HTMLExt myEntriesLink = new HTMLExt("<a href=\"javascript:void(0);\">mis registros</a>");
			HTMLExt logoutLink = new HTMLExt("<a href=\"javascript:void(0);\">salir</a>");
			HorizontalPanel horizontalPanel = new HorizontalPanel();
			PopupOverlayModel popupOvelayModel = new PopupOverlayModel();
			MyProfileWidget myProfileWidget = new MyProfileWidget(authenticationModel);
			popupOvelayModel.setContentWidget(myProfileWidget);
			PopupOverlayWidget popupOverlay = new PopupOverlayWidget(popupOvelayModel);
			MyEntriesWidget myEntriesWidget = new MyEntriesWidget(myEntriesModel, authenticationModel);
			
//			horizontalPanel.setSpacing(5);
			horizontalPanel.add(titleHtml);
			horizontalPanel.add(myProfileLink);
			horizontalPanel.add(myEntriesLink);
			horizontalPanel.add(logoutLink);
			
			userController.registerUserWidget(this);
			userController.registerTitleHtml(titleHtml);
			userController.registerMyProfileLink(myProfileLink);
			userController.registerAuthenticationModel(authenticationModel);
			userController.registerLogoutLink(logoutLink);
			userController.registerPopupOverlayModel(popupOvelayModel);
			userController.registerMyEntriesLink(myEntriesLink);
			userController.registerMyProfileWidget(myProfileWidget);
			userController.registerMyEntriesWidget(myEntriesWidget);
			userController.registerMyEntriesModel(myEntriesModel);
			userController.registerEventManager(EventManagerImpl.getInstance());
			userController.registerJsUtil(JsUtil.getInstance());
			
			setStyleName(XelavosResources.INSTANCE.css().userWidget());
			titleHtml.addStyleName(XelavosResources.INSTANCE.css().titleHtml());
			return horizontalPanel;
		}
		
	}
	
	private class LoginWidget extends LazyWidget{
		
		private LoginController loginController = new LoginController();
		
		@Override
		public Widget createContentWidget() {
			HorizontalPanel mainPanel = new HorizontalPanel();
			PrefilledTextBoxModel emailTextBoxModel = new PrefilledTextBoxModel();
			emailTextBoxModel.setDefaultText("<span class=\"main\">email</span> ej carlos.lopez@gmail.com");
			PrefilledTextBox emailTextBox = new PrefilledTextBox(emailTextBoxModel);
			PrefilledTextBoxModel passwordTextBoxModel = new PrefilledTextBoxModel();
			passwordTextBoxModel.setDefaultText("<span class=\"main\">contraseña</span>");
			PasswordTextBoxExt passwordTextBoxExt = new PasswordTextBoxExt();
			PrefilledTextInput passwordTextBox = new PrefilledTextInput(passwordTextBoxModel, passwordTextBoxExt);
			ButtonExt loginButton = new ButtonExt("login");
			HTMLExt createAccountLink = new HTMLExt("<a href=\"javascript:void(0);\">crear cuenta</a>");
			FlowPanel leftPanel = new FlowPanel();
//			mainPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
//			mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
			HTMLExt forgotPasswordLink = new HTMLExt("<a href=\"javascript:void(0);\">olvidó su contraseña?</a>");
			HTMLExt validateAccountLink = new HTMLExt("<a href=\"javascript:void(0);\">confirmar direccion electronica</a>");
			
			leftPanel.add(emailTextBox);
			leftPanel.add(passwordTextBox);
			
			mainPanel.add(leftPanel);
			mainPanel.add(loginButton);
			mainPanel.add(createAccountLink);
			mainPanel.add(forgotPasswordLink);
			mainPanel.add(validateAccountLink);
			
			setStyleName(XelavosResources.INSTANCE.css().loginWidget());
			
			authenticationController.registerCreateAccountLink(createAccountLink);
			authenticationController.registerEventManager(EventManagerImpl.getInstance());
			loginController.registerLoginWidget(this);
			loginController.registerForgotPasswordLink(forgotPasswordLink);
			loginController.registerValidateAccountLink(validateAccountLink);
			loginController.registerLoginButton(loginButton);
			loginController.registerEmailTextBoxModel(emailTextBoxModel);
			loginController.registerPasswordTextBox(passwordTextBoxExt);
			loginController.registerPasswordTextBoxModel(passwordTextBoxModel);
			loginController.registerAuthenticationModel(authenticationModel);
			loginController.registerJsUtil(JsUtil.getInstance());
			
			return mainPanel;
		}
	}
	
	private AuthenticationController authenticationController = new AuthenticationController();
	private final AuthenticationModel authenticationModel;
	private final MyEntriesModel myEntriesModel;

	public AuthenticationWidget(AuthenticationModel authenticationModel, MyEntriesModel myEntriesModel) {
		this.authenticationModel = authenticationModel;
		this.myEntriesModel = myEntriesModel;
	}
	
	@Override
	public Widget createContentWidget() {
		HorizontalPanelExt mainPanel = new HorizontalPanelExt();
		LoginWidget loginWidget = new LoginWidget();
		PopupOverlayModel createAccountPopupModel = new PopupOverlayModel();
		CreateAccounWidget createAccountWidget = new CreateAccounWidget(createAccountPopupModel);
		createAccountPopupModel.setContentWidget(createAccountWidget);
		PopupOverlayWidget createAccountPopup = new PopupOverlayWidget(createAccountPopupModel);
		HTMLExt messageHtml = new HTMLExt();
		RoundBorderCssResource css = RoundBorderResources.Util.INSTANCE.css();
		RoundBorderModel roundBorderModel = new RoundBorderModel();
		roundBorderModel.setContentWidget(mainPanel);
		roundBorderModel.setCssResource(css);
		RoundBorder decoratorPanel = new RoundBorder(roundBorderModel);

		mainPanel.add((Widget)loginWidget);
		mainPanel.add((Widget)messageHtml);
		
		setStyleName(XelavosResources.INSTANCE.css().authenticationWidget());
		messageHtml.addStyleName(XelavosResources.INSTANCE.css().messageHtml());
		
		authenticationController.registerAuthenticationModel(authenticationModel);
		authenticationController.registerAuthenticationWidget(this);
		authenticationController.registerLoginWidget(loginWidget);
		authenticationController.registerCreateAccountWidget(createAccountWidget);
		authenticationController.registerMessageHtml(messageHtml);
		authenticationController.registerCreateAccountPopupModel(createAccountPopupModel);
		return decoratorPanel;
	}

}
